﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace AllenalaMente.Games.Calculator
{
    public partial class Calculator : UserControl, GameInterface
    {
        private DispatcherTimer updateTimer;
        private Random random;
        private Rect gameBounds;
        private int score, difficulty = 1;
        private int giuste, sbagliate;
        private UITimer gameTimer;

        public Calculator()
        {
            InitializeComponent();

            // Initalize game
            random = new Random();
            gameBounds = new Rect(0, 0, LayoutRoot.Width, LayoutRoot.Height);
            score = 0;
            textBlockScore.Text = score.ToString();

            // Update timer
            updateTimer = new DispatcherTimer();
            updateTimer.Interval = TimeSpan.FromMilliseconds(20);
            updateTimer.Tick += new EventHandler(updateTimer_Tick);
            updateTimer.Start();

            // Game timer
            gameTimer = new UITimer(60);
            LayoutRoot.Children.Add(gameTimer);
            gameTimer.timeOut += new EventHandler(game_TimeOut);
            gameTimer.Start();

            CreateEquation();
        }


        #region Metodi

        int result = 0;
        int solution = 0;
        private List<string> numbers = new List<string>();

        enum Operators
        {
            Plus,
            Minus, 
            Divide,
            Multiply
        }

        private void CreateEquation()
        {
            numbers.Clear();
            textBlockEquation.Text = "";
            rectangle2.Fill = new SolidColorBrush(Colors.White);

            int n1 = random.Next(0, 11);
            int n2 = random.Next(0, 11);

            result = 0;

            Operators op = (Operators)random.Next(0, 4);

            string oper = ""; 

            switch (op)
            {
                case Operators.Plus:
                    result = n1 + n2;
                    oper = "+";
                    break;

                case Operators.Minus:
                    if (n1 < n2) { n2 = n1; n1 = n2 + random.Next(0, n2); }
                    result = n1 - n2;
                    oper = "-";
                    break;

                case Operators.Multiply:
                    result = n1 * n2;
                    oper = "X";
                    break;

                case Operators.Divide:
                    if (n1 == 0) n1++;
                    if (n2 == 0) n2++;
                    if (n1 < n2) n2 /= n1;
                    if (n1 % n2 != 0) n1 = n2 * random.Next(1, 10);
                    result = n1 / n2;
                    oper = "/";
                    break;
            }

            // Difficoltà 2
            string oper2 = "";
            int n3 = random.Next(0, 11);

            if (difficulty == 2)
            {
                n2 = result;

                Operators op2 = (Operators)random.Next(0, 4);
                switch (op2)
                {
                    case Operators.Plus:
                        result = n2 + n3;
                        oper2 = "+";
                        break;

                    case Operators.Minus:
                        if (n2 < n3) { n3 = random.Next(0, n2); }
                        result = n2 - n3;
                        oper2 = "-";
                        break;

                    case Operators.Multiply:
                        result = n2 * n3;
                        oper2 = "X";
                        break;

                    case Operators.Divide:
                        //if (n2 == 0) n2++;
                        if (n3 == 0) n3 = 1;
                        if (n2 < n3) n3 /= n2;
                        if (n2 % n3 != 0) n3 = n2 / random.Next(1, 5);
                        result = n2 / n3;
                        oper2 = "/";
                        break;
                }
            }

            numbers.Add(n1.ToString());
            numbers.Add(n2.ToString());
            if(difficulty == 2) numbers.Add(n3.ToString());
            numbers.Add(result.ToString());

            int ran = random.Next(0, numbers.Count());
            solution = int.Parse(numbers[ran]);
            numbers[ran] = "?";

            textBlockEquation.Text = "" + numbers[0] + " " + oper + " " + numbers[1] + " = " + numbers[2];// +"     (" + solution + ")";

            if (difficulty == 2) textBlockEquation.Text = "(" + numbers[0] + " " + oper + " " + numbers[1] + ") " + oper2 + " " + numbers[2] + " = " + numbers[3] + "     (" + solution + ")";

        }

        private void CheckSolution(string sol)
        {
            if (sol != "")
            {
                if (int.Parse(sol) == solution)
                {
                    // Punti: 100 + (diff.lev * 10)
                    IncreaseScore(100 + (difficulty * 10));

                    rectangle2.Fill = new SolidColorBrush(Colors.Green);

                    giuste++;

                    if (giuste % 3 == 0) difficulty++;
                }
                else
                {
                    rectangle2.Fill = new SolidColorBrush(Colors.Red);
                    sbagliate++;
                }

                CreateEquation();
            }
        }

        private void IncreaseScore(int amount)
        {
            score += amount;
            textBlockScore.Text = score.ToString();
        }


        #endregion


        #region Eventi

        public event GameEvent GameEnd;

        private void updateTimer_Tick(object sender, EventArgs e)
        {
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            switch (((Button)sender).Name)
            {
                case "button0":
                    textBlockNumber.Text += "0";
                    break;
                case "button1":
                    textBlockNumber.Text += "1";
                    break;
                case "button2":
                    textBlockNumber.Text += "2";
                    break;
                case "button3":
                    textBlockNumber.Text += "3";
                    break;
                case "button4":
                    textBlockNumber.Text += "4";
                    break;
                case "button5":
                    textBlockNumber.Text += "5";
                    break;
                case "button6":
                    textBlockNumber.Text += "6";
                    break;
                case "button7":
                    textBlockNumber.Text += "7";
                    break;
                case "button8":
                    textBlockNumber.Text += "8";
                    break;
                case "button9":
                    textBlockNumber.Text += "9";
                    break;
                case "buttonC":
                    textBlockNumber.Text = "";
                    break;
                case "buttonEnter":
                    CheckSolution(textBlockNumber.Text);
                    textBlockNumber.Text = "";
                    break;
            }
        }

        private void game_TimeOut(object sender, EventArgs e)
        {
            // Finito il tempo
            GameEventArgs gea = new GameEventArgs();
            gea.reason = GameCloseReason.Timeout;
            gea.score = this.score;
            gea.giuste = giuste;
            gea.sbagliate = sbagliate;
            if (GameEnd != null)
                GameEnd(this, gea);
        }

        #endregion

        
    }
}
